import router from '@system.router';
import prompt from '@system.prompt';

export default {
    data: {
        title: '',
        agree: false,
        mobile: '',
        canGetCode: false, //can get code
        retryCode: '获取验证码',
        getting_code: false, //获取验证码中
        code: '',
        sent: false, //验证码已经发送
        remain_time: 3, //倒计时剩余时间
        //是否显示剩余时间
        showRemainTime: false,
        timerID: 0, //计时器的序号
    },
    //倒计时算法
    countDown() {
        if (this.remain_time != 0) {
            this.remain_time -= 1;
        } else {
            this.remain_time = 3;
            clearInterval(this.timerID)
            this.retryCode = '重发验证码'
            this.sent = false;
            this.canGetCode = true;
            this.showRemainTime = !this.showRemainTime;
        }
    },
    back() {
        router.back();
    },
    gettingCode() {
        this.getting_code = !this.getting_code;
        this.canGetCode = false;
        setTimeout(this.checkCode, 2000);
    },
    checkCode() {
        if (this.mobile == '11111111111') {
            this.code = "3497";
            this.sent = true;
            this.showRemainTime = true;
            this.timerID = setInterval(this.countDown, 1000);

            prompt.showToast({
                message: '验证码已发送'
            })
        } else {
            prompt.showToast({
                message: '手机号不存在！'
            })
            this.canGetCode = true;
        }
        this.getting_code = false;
    },
    checkAgree() { //点击选中或者取消选中隐私协议
        this.agree = !this.agree;
        this.validateInput();
    },
    changePhone(event) { //当用户输入时保存用户的帐号输入值
        this.mobile = event.value;
        this.validateInput();
    },
    validateInput() {
        //用户不满足输入有效性
        if (this.mobile.length < 11 ||
        !this.mobile.startsWith("1")) {
            this.canGetCode = false;
        } else { //满足输入有效性
            if (this.agree) {
                this.canGetCode = true;
            } else {
                this.canGetCode = false;
            }
        }
    },
    navToPrivacy() {
        router.push({
            uri: "pages/privacy/privacy" /*跳转到隐私页面*/
        })
    },
    navToAgreement() {
        router.push({
            uri: "pages/agreement/agreement" /*跳转到服务条款页面*/
        })
    }
}
